package com.lv.suyiyong.hfrwork.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.lv.common.base.BaseDialog;
import com.lv.common.dialog.manager.ChainNode;
import com.lv.common.dialog.manager.DialogChain;
import com.lv.common.imp.OnDialogListener;
import com.lv.common.utils.Constants;
import com.lv.service.bean.ClickBean;
import com.lv.service.bean.CoorDinateBean;
import com.lv.service.bean.VersionUpdate;
import com.lv.suyiyong.hfrwork.R;

import java.lang.reflect.Type;


/**
 * 作者： ygx
 * 创建日期：2022/11/17 2:35 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：版本更新的弹窗
 */
public class VersionUpdateDialog extends BaseDialog implements ChainNode {
    private Context mContext;
    private RelativeLayout rlParent;
    private ImageView mImageView;
    private View mViewClick, mViewClose;
    private VersionUpdate mVersionListBean;

    public VersionUpdateDialog(Context context, boolean isCancelable, boolean isBackCancelable) {
        super(context, isCancelable, isBackCancelable);
        this.mContext = context;
    }

    public VersionUpdateDialog(Context context, VersionUpdate versionListBean, boolean isCancelable, boolean isBackCancelable, OnDialogListener onDialogListener) {
        super(context, isCancelable, isBackCancelable, Gravity.CENTER, true, true);
        this.mContext = context;
        this.mVersionListBean = versionListBean;
        this.onDialogListener = onDialogListener;
    }

    public VersionUpdateDialog(Context context, boolean isCancelable, boolean isBackCancelable, boolean widthIsMatch, boolean heightIsMatch) {
        super(context, isCancelable, isBackCancelable, Gravity.CENTER, widthIsMatch, heightIsMatch);
        this.mContext = context;
    }

    @Override
    protected void initView() {
        rlParent = findViewById(R.id.rl_parent);
        mImageView = findViewById(R.id.iv_image);
        mViewClick = findViewById(R.id.view_click);
        mViewClose = findViewById(R.id.view_close);
        String height = mVersionListBean.getHeight();
        String width = mVersionListBean.getWidth();
        int parseIntW = Integer.parseInt(width) / 3;
        int parseIntH = Integer.parseInt(height) / 3;
        RelativeLayout.LayoutParams ivLayoutParams = (RelativeLayout.LayoutParams) mImageView.getLayoutParams();
        ivLayoutParams.width = SizeUtils.dp2px(parseIntW);
        ivLayoutParams.height = SizeUtils.dp2px(parseIntH);
        mImageView.setLayoutParams(ivLayoutParams);
        Glide.with(mContext).load(mVersionListBean.getImage()).into(mImageView);
        String coordinate = mVersionListBean.getCoordinate();
        LogUtils.e(coordinate);
        Type type = new TypeToken<CoorDinateBean>() {
        }.getType();
        CoorDinateBean coorDinateBean = GsonUtils.fromJson(coordinate, type);
        ClickBean clickBean = coorDinateBean.getClick();
        float aDoubleX = clickBean.getX() * 1.0f / 3;
        float aDoubleXX = clickBean.getW() * 1.0f / 3;
        float aDoubleY = clickBean.getY() * 1.0f / 3;
        float aDoubleYY = clickBean.getH() * 1.0f / 3;
        ClickBean closeBean = coorDinateBean.getClose();
        float aDoubleCloseX = closeBean.getX() * 1.0f / 3;
        float aDoubleCloseXX = closeBean.getW() * 1.0f / 3;
        float aDoubleCloseY = closeBean.getY() * 1.0f / 3;
        float aDoubleCloseYY = closeBean.getH() * 1.0f / 3;
        RelativeLayout.LayoutParams clickParams = (RelativeLayout.LayoutParams) mViewClick.getLayoutParams();
        clickParams.leftMargin = SizeUtils.dp2px(aDoubleX);
        clickParams.topMargin = SizeUtils.dp2px(aDoubleY);
        clickParams.width = SizeUtils.dp2px(aDoubleXX);
        clickParams.height = SizeUtils.dp2px(aDoubleYY);
        mViewClick.setLayoutParams(clickParams);
        RelativeLayout.LayoutParams closeParams = (RelativeLayout.LayoutParams) mViewClose.getLayoutParams();
        closeParams.leftMargin = SizeUtils.dp2px(aDoubleCloseX);
        closeParams.topMargin = SizeUtils.dp2px(aDoubleCloseY);
        closeParams.width = SizeUtils.dp2px(aDoubleCloseXX);
        closeParams.height = SizeUtils.dp2px(aDoubleCloseYY);
        mViewClose.setLayoutParams(closeParams);

        if (Constants.isShowPicAlpha) {
            mViewClick.setBackgroundColor(Color.parseColor("#00CC90"));
            mViewClick.setAlpha(0.3f);
            mViewClose.setBackgroundColor(Color.parseColor("#00CC90"));
            mViewClose.setAlpha(0.3f);
        }
        initEvnet();
    }

    private void initEvnet() {

        mViewClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDialogListener != null) {
                    onDialogListener.onSureClick("");
                }
                dismiss();
            }
        });
        if (mVersionListBean.getIscompel() == null || mVersionListBean.getIscompel().equals("0")) {
            rlParent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onDialogListener != null) {
                        onDialogListener.onCancelClick();
                    }
                    dismiss();
                }
            });
        }
        mViewClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDialogListener != null) {
                    onDialogListener.onCancelClick();
                }
                dismiss();
            }
        });

    }


    @Override
    protected int getLayoutId() {
        return R.layout.dialog_image;
    }

    private OnDialogListener onDialogListener;


    public void setOnDialogListener(OnDialogListener onDialogListener) {
        this.onDialogListener = onDialogListener;
    }

    private int mChainId = 0;

    @Override
    public int getId() {
        return mChainId;
    }

    private void setChainId(int chainId) {
        mChainId = chainId;
    }

    @Override
    public void proceed(@Nullable DialogChain chain) {
        show();
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (chain != null) {
                    chain.proceed();
                }
            }
        });
    }
}
